package com.huibao.hulibao.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.example.zhouwei.library.CustomPopWindow;
import com.huibao.hulibao.R;
import com.huibao.hulibao.activity.ChangeLoginNameActivity;
import com.huibao.hulibao.activity.CheckActivity;
import com.huibao.hulibao.activity.LoginActivity;
import com.huibao.hulibao.activity.MainActivity;
import com.huibao.hulibao.activity.MyBindClientActivity;
import com.huibao.hulibao.activity.VersionActivity;
import com.huibao.hulibao.activity.WebViewActivity;
import com.huibao.hulibao.adapter.StartHuliLvAdapter;
import com.huibao.hulibao.adapter.WebLvAdapter;
import com.huibao.hulibao.adapter.ZhangHaoLvAdapter;
import com.huibao.hulibao.constant.GlobalConstants;
import com.huibao.hulibao.entity.AllLoginEntity;
import com.huibao.hulibao.entity.HuLiLogEntity;
import com.huibao.hulibao.entity.LoginEntity;
import com.huibao.hulibao.entity.WebEntity;
import com.huibao.hulibao.utils.MyUtils;
import com.huibao.hulibao.utils.PreUtils;
import com.huibao.hulibao.utils.T;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by lizhaoyong on 2018/5/7.
 */
public class MoreFragmentView extends Fragment {
    private Unbinder unbinder;

    private Context context;

    @BindView(R.id.more_lv_webview)
    ListView more_lv_webview;
    @BindView(R.id.rl_my_client)
    RelativeLayout rl_my_client;
    @BindView(R.id.tv_quite)
    TextView tv_quite;
    @BindView(R.id.tv_current_login_name)
    TextView tv_current_login_name;
    @BindView(R.id.tv_version)
    TextView tv_version;

    public LoginEntity le;
    public WebEntity we;

    @OnClick(R.id.rl_version)
    public void enterVersion() {

        Intent intent = new Intent(getActivity(), WebViewActivity.class);
        // web页面调试
        // intent.putExtra("url", "https://www.pgyer.com/hpda2");
        if (le.Message.SysVersion != null) {
            // LogUtils.d("SysVersion = " + le.Message.SysVersion);
            intent.putExtra("url", GlobalConstants.URL_GUANWANG + MyUtils.getAppVersionName(getActivity()) + "&sv=" + le.Message.SysVersion);
        } else {
            intent.putExtra("url", GlobalConstants.URL_GUANWANG + MyUtils.getAppVersionName(getActivity()));
        }

        // intent.putExtra("url", "https://www.baidu.com/");
        intent.putExtra("title", getResources().getString(R.string.app_name));
        startActivity(intent);
    }

    @OnClick(R.id.login_change)
    public void enterChangeLoginName() {
        Intent intent = new Intent(getActivity(), ChangeLoginNameActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.rl_location)
    public void check() {
        Intent intent = new Intent(getActivity(), CheckActivity.class);
        startActivity(intent);
    }


    @OnClick(R.id.tv_quite)
    public void quite() {
        PreUtils.saveObject(context, "LoginEntity", null);
        Intent intent = new Intent(context, LoginActivity.class);
        startActivity(intent);
        getActivity().finish();

    }


    @OnClick(R.id.rl_my_client)
    public void enterBindActivity() {
        Intent intent = new Intent(context, MyBindClientActivity.class);
        startActivity(intent);
    }


    public void getWebList() {
        RequestParams params = new RequestParams(GlobalConstants.URL);
        params.setCacheMaxAge(0);
        params.addBodyParameter("q", le.Message.Session + GlobalConstants.URL_WEB_LIST + "-");

        // LogUtils.d("params = " + params.toString());

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(result);
                    // LogUtils.d("result = " + jsonObject.toString());
                    we = GlobalConstants.gson.fromJson(jsonObject.toString(), WebEntity.class);

                    if (we != null && we.Message.size() > 0) {
                        more_lv_webview.setAdapter(new WebLvAdapter(we.Message, getActivity()));
                        // 跳转到webview
                        jumpToWebView(we);
                        more_lv_webview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Intent intent = new Intent(getActivity(), WebViewActivity.class);
                                WebEntity.DetailData wedd = we.Message.get(position);
                                if (wedd.url.contains("http://") || wedd.url.contains("https://")) {
                                    intent.putExtra("url", wedd.url);
                                } else {
                                    intent.putExtra("url", GlobalConstants.HOST + wedd.url.replace("{session}", le.Message.Session));
                                    // intent.putExtra("url", GlobalConstants.HOST + "/AppWap/PDA_APP/CRM/Index.aspx?ss=" + le.Message.Session);
                                }
                                intent.putExtra("title", getResources().getString(R.string.app_name));
                                startActivity(intent);
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    // 根据推送的内容跳转到webview
    public void jumpToWebView(WebEntity we) {
        MainActivity mainActivity = (MainActivity) getActivity();
        String web_json = mainActivity.web_json;
        if (web_json != null && !web_json.equals("")) {
            String str = MyUtils.replaceJson(web_json);
            // LogUtils.d("str = " + str);
            String[] str_arr = str.split(",");
            String tag = str_arr[1].split(":")[1];
            String[] info_arr = str_arr[0].split(":");
            for (int i = 0; i < we.Message.size(); i++) {
                WebEntity.DetailData wedd = we.Message.get(i);
                if (we.Message.get(i).tag.equals(tag)) {
                    Intent intent = new Intent(getActivity(), WebViewActivity.class);
                    if (wedd.url.contains("http://") || wedd.url.contains("https://")) {
                        intent.putExtra("url", wedd.url);
                    } else {
                        intent.putExtra("url", GlobalConstants.HOST + wedd.url.replace("{session}", le.Message.Session) + "&" + info_arr[0] + "=" + info_arr[1]);
                    }
                    intent.putExtra("title", getResources().getString(R.string.app_name));
                    startActivity(intent);
                    break;
                }
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_more, null);
        unbinder = ButterKnife.bind(this, v);
        context = getActivity();
        // 判断用户是否登录
        le = (LoginEntity) PreUtils.getObject(context, "LoginEntity");
        if (le == null) {
            Intent intent = new Intent(context, LoginActivity.class);
            startActivity(intent);
            getActivity().finish();
        } else {
            tv_current_login_name.setText(le.Message.LoginName);
            getWebList();
        }

        tv_version.setText(getResources().getString(R.string.version) + MyUtils.getAppVersionName(getActivity()));

        return v;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
